package com.iot;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.iot.entity.*;
import com.iot.service.*;
import com.model.*;
import common.utils.constant.ConstantBasketBallUrl;
import common.utils.constant.ConstantFootBallUrl;
import common.utils.http.HttpUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.HashMap;
import java.util.List;

@SpringBootApplication
@MapperScan({"com.iot.**.mapper"})
public class ClientApplication implements CommandLineRunner {

  @Autowired
  private CategoryNanoService categoryNanoService;

  @Autowired
  private CityNanoService cityNanoService;

  @Autowired
  private LeagueNanoService leagueNanoService;

  @Autowired
  private CompetitionNanoService competitionNanoService;

  @Autowired
  private TeamNanoService teamNanoService;

  @Autowired
  private PlayerNanoService playerNanoService;

  @Autowired
  private FootballPlayerNanoService footballPlayerNanoService;

  @Autowired
  private BasketballPlayerNanoService basketballPlayerNanoService;

  @Autowired
  private SeasonNanoService seasonNanoService;

  @Autowired
  private CoachNanoService coachNanoService;

  @Autowired
  private HonorNanoService honorNanoService;

  @Autowired
  private FootballRefereeNanoService footballRefereeNanoService;

  @Autowired
  private StageNanoService stageNanoService;

  @Autowired
  private VenueNanoService venueNanoService;

  @Autowired
  private MatchesNanoService matchesNanoService;
  public static void main(String[] args) {

    SpringApplication.run(ClientApplication.class, args);
  }

  @Override
  public void run(String... args) throws Exception {

    HashMap<String,String> paraMap =new HashMap<>();
    paraMap.put("user","szsmty");
    paraMap.put("secret","c561259f6722a667f3938ae99019b4f3");
//    grabCategory(paraMap);
//    grabCity(paraMap);
//    grabLeagueNano(paraMap,1);
//    grabLeagueNano(paraMap,2);
//    grabCompetitionRuleNano(paraMap);
//    grabSeasonNano(paraMap,1);
//    grabSeasonNano(paraMap,2);
//    grabTeamNano(paraMap,1);
//    grabTeamNano(paraMap,2);
//    grabPlayerNano(paraMap,1);
    grabPlayerNano(paraMap,2);
    grabHonorNano(paraMap,1);
    grabHonorNano(paraMap,2);
    grabCoachNano(paraMap,1);
    grabCoachNano(paraMap,2);
    grabVenueNano(paraMap,1);
    grabVenueNano(paraMap,2);
    grabStageNano(paraMap,1);
    grabStageNano(paraMap,2);
    grabRefereeNano(paraMap);
    grabMatchNano(paraMap,1);
    grabMatchNano(paraMap,2);
  }


  public void grabCategory(HashMap<String,String> paraMap) throws Exception{
    String rtn = HttpUtils.sendGet(ConstantFootBallUrl.football_category_url,paraMap);
    System.out.println(rtn);
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
    CategoryNanoVO returnMessage = objectMapper.readValue(rtn, CategoryNanoVO.class);
    System.out.println(JSON.toJSONString(returnMessage));
    categoryNanoService.insertOrUpdateAllColumnBatch(returnMessage.getResults());
  }


  public void grabCity(HashMap<String,String> paraMap) throws Exception{
    String rtn = HttpUtils.sendGet(ConstantFootBallUrl.football_country_url,paraMap);
    System.out.println(rtn);
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
    CityNanoVO returnMessage = objectMapper.readValue(rtn, CityNanoVO.class);
    System.out.println(JSON.toJSONString(returnMessage));
    cityNanoService.insertOrUpdateAllColumnBatch(returnMessage.getResults());
  }

  public void grabLeagueNano(HashMap<String,String> paraMap,Integer sportId) throws Exception{

    List<LeagueNano> dataList =null;
    int id =0;
    String url ="";
    if(sportId==1){
      url=ConstantFootBallUrl.football_competition_url;
    }else{
      url= ConstantBasketBallUrl.basketball_competition_url;
    }
    do{
      paraMap.put("id",id+"");
      String rtn = HttpUtils.sendGet(url,paraMap);
      System.out.println(rtn);
      ObjectMapper objectMapper = new ObjectMapper();
      objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
      LeagueNanoVO returnMessage = objectMapper.readValue(rtn, LeagueNanoVO.class);
      System.out.println(JSON.toJSONString(returnMessage));
      dataList =returnMessage.getResults();
      id=returnMessage.getQuery().getMaxId()+1;
      if(dataList.isEmpty()) continue;
      dataList.stream().forEach(s -> s.setSportId(sportId));
      leagueNanoService.insertOrUpdateAllColumnBatch(dataList);
    }while(dataList !=null &&dataList.size()>0);

  }

  public void grabCompetitionRuleNano(HashMap<String,String> paraMap) throws Exception{

    List<CompetitionNano> dataList =null;
    int id =0;
    String url =ConstantFootBallUrl.football_competition_rule_url;
    do{
      paraMap.put("id",id+"");
      String rtn = HttpUtils.sendGet(url,paraMap);
      System.out.println(rtn);
      ObjectMapper objectMapper = new ObjectMapper();
      objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
      CompetitionRoleNanoVO returnMessage = objectMapper.readValue(rtn, CompetitionRoleNanoVO.class);
      System.out.println(JSON.toJSONString(returnMessage));
      dataList =returnMessage.getResults();
      id=returnMessage.getQuery().getMaxId()+1;
      if(dataList.isEmpty()) continue;
      competitionNanoService.insertOrUpdateAllColumnBatch(dataList);
    }while(dataList !=null &&dataList.size()>0);

  }


  public void grabTeamNano(HashMap<String,String> paraMap,Integer sportId) throws Exception{

    List<TeamNano> dataList =null;
    int id =0;
    String url ="";
    if(sportId==1){
      url=ConstantFootBallUrl.football_team_url;
    }else{
      url= ConstantBasketBallUrl.basketball_team_url;
    }
    do{
      paraMap.put("id",id+"");
      String rtn = HttpUtils.sendGet(url,paraMap);
      System.out.println(rtn);
      ObjectMapper objectMapper = new ObjectMapper();
      objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
      TeamNanoVO returnMessage = objectMapper.readValue(rtn, TeamNanoVO.class);
      System.out.println(JSON.toJSONString(returnMessage));
      dataList =returnMessage.getResults();
      id=returnMessage.getQuery().getMaxId()+1;
      if(dataList.isEmpty()) continue;
      dataList.stream().forEach(s -> s.setSportId(sportId));
      teamNanoService.insertOrUpdateAllColumnBatch(dataList);
    }while(dataList !=null &&dataList.size()>0);

  }

  public void grabPlayerNano(HashMap<String,String> paraMap,Integer sportId) throws Exception{

    int id =0;
    String url ="";
    List dataList=null;
    if(sportId==1){
      url=ConstantFootBallUrl.football_player_url;
      do{
        paraMap.put("id",id+"");
        String rtn = HttpUtils.sendGet(url,paraMap);
        System.out.println(rtn);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        FootballPlayerNanoVO returnMessage = objectMapper.readValue(rtn, FootballPlayerNanoVO.class);
        System.out.println(JSON.toJSONString(returnMessage));
        dataList =returnMessage.getResults();
        id=returnMessage.getQuery().getMaxId()+1;
        if(dataList.isEmpty()) continue;
        footballPlayerNanoService.insertOrUpdateAllColumnBatch(dataList);
      }while(dataList !=null &&dataList.size()>0);
    }else{
      url= ConstantBasketBallUrl.basketball_player_url;
      do{
        paraMap.put("id",id+"");
        String rtn = HttpUtils.sendGet(url,paraMap);
        System.out.println(rtn);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        BasketballPlayerNanoVO returnMessage = objectMapper.readValue(rtn, BasketballPlayerNanoVO.class);
        System.out.println(JSON.toJSONString(returnMessage));
        dataList =returnMessage.getResults();
        id=returnMessage.getQuery().getMaxId()+1;
        if(dataList.isEmpty()) continue;
        basketballPlayerNanoService.insertOrUpdateAllColumnBatch(dataList);
      }while(dataList !=null &&dataList.size()>0);
    }


  }

  public void grabSeasonNano(HashMap<String,String> paraMap,Integer sportId) throws Exception{

    List<SeasonNano> dataList =null;
    int id =0;
    String url ="";
    if(sportId==1){
      url=ConstantFootBallUrl.football_season_url;
    }else{
      url= ConstantBasketBallUrl.basketball_season_url;
    }
    do{
      paraMap.put("id",id+"");
      String rtn = HttpUtils.sendGet(url,paraMap);
      ObjectMapper objectMapper = new ObjectMapper();
      objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
      SeasonNanoVO returnMessage = objectMapper.readValue(rtn, SeasonNanoVO.class);
      System.out.println(JSON.toJSONString(returnMessage));
      dataList =returnMessage.getResults();
      id=returnMessage.getQuery().getMaxId()+1;
      if(dataList.isEmpty()) continue;
      dataList.stream().forEach(s -> s.setSportId(sportId));
      seasonNanoService.insertOrUpdateAllColumnBatch(dataList);
    }while(dataList !=null &&dataList.size()>0);

  }


  public void grabCoachNano(HashMap<String,String> paraMap,Integer sportId) throws Exception{

    List<CoachNano> dataList =null;
    int id =0;
    String url ="";
    if(sportId==1){
      url=ConstantFootBallUrl.football_coach_url;
    }else{
      url= ConstantBasketBallUrl.basketball_coach_url;
    }
    do{
      paraMap.put("id",id+"");
      String rtn = HttpUtils.sendGet(url,paraMap);
      ObjectMapper objectMapper = new ObjectMapper();
      objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
      CoachVO returnMessage = objectMapper.readValue(rtn, CoachVO.class);
      System.out.println(JSON.toJSONString(returnMessage));
      dataList =returnMessage.getResults();
      id=returnMessage.getQuery().getMaxId()+1;
      if(dataList.isEmpty()) continue;
      dataList.stream().forEach(s -> s.setSportId(sportId));
      coachNanoService.insertOrUpdateAllColumnBatch(dataList);
    }while(dataList !=null &&dataList.size()>0);

  }


  public void grabHonorNano(HashMap<String,String> paraMap,Integer sportId) throws Exception{

    List<HonorNano> dataList =null;
    int id =0;
    String url ="";
    if(sportId==1){
      url=ConstantFootBallUrl.football_honor_url;
    }else{
      url= ConstantBasketBallUrl.basketball_honor_url;
    }
    do{
      paraMap.put("id",id+"");
      String rtn = HttpUtils.sendGet(url,paraMap);
      ObjectMapper objectMapper = new ObjectMapper();
      objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
      HonorVO returnMessage = objectMapper.readValue(rtn, HonorVO.class);
      System.out.println(JSON.toJSONString(returnMessage));
      dataList =returnMessage.getResults();
      id=returnMessage.getQuery().getMaxId()+1;
      if(dataList.isEmpty()) continue;
      dataList.stream().forEach(s -> s.setSportId(sportId));
      honorNanoService.insertOrUpdateAllColumnBatch(dataList);
    }while(dataList !=null &&dataList.size()>0);

  }

  public void grabRefereeNano(HashMap<String,String> paraMap) throws Exception{

    List<FootballRefereeNano> dataList =null;
    int id =0;
    do{
      paraMap.put("id",id+"");
      String rtn = HttpUtils.sendGet(ConstantFootBallUrl.football_referee_url,paraMap);
      ObjectMapper objectMapper = new ObjectMapper();
      objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
      RefereeVO returnMessage = objectMapper.readValue(rtn, RefereeVO.class);
      System.out.println(JSON.toJSONString(returnMessage));
      dataList =returnMessage.getResults();
      id=returnMessage.getQuery().getMaxId()+1;
      if(dataList.isEmpty()) continue;
      footballRefereeNanoService.insertOrUpdateAllColumnBatch(dataList);
    }while(dataList !=null &&dataList.size()>0);

  }

  public void grabStageNano(HashMap<String,String> paraMap,Integer sportId) throws Exception{

    List<StageNano> dataList =null;
    int id =0;
    do{
      paraMap.put("id",id+"");
      String url ="";
      if(sportId==1){
        url=ConstantFootBallUrl.football_stage_url;
      }else{
        url= ConstantBasketBallUrl.basketball_stage_url;
      }
      String rtn = HttpUtils.sendGet(url,paraMap);
      ObjectMapper objectMapper = new ObjectMapper();
      objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
      StageVO returnMessage = objectMapper.readValue(rtn, StageVO.class);
      System.out.println(JSON.toJSONString(returnMessage));
      dataList =returnMessage.getResults();
      dataList.stream().forEach(s -> s.setSportId(sportId));
      id=returnMessage.getQuery().getMaxId()+1;
      if(dataList.isEmpty()) continue;

      stageNanoService.insertOrUpdateAllColumnBatch(dataList);
    }while(dataList !=null &&dataList.size()>0);

  }

  public void grabVenueNano(HashMap<String,String> paraMap,Integer sportId) throws Exception{

    List<VenueNano> dataList =null;
    int id =0;
    String url ="";
    if(sportId==1){
      url=ConstantFootBallUrl.football_venue_url;
    }else{
      url= ConstantBasketBallUrl.basketball_venue_url;
    }
    do{
      paraMap.put("id",id+"");
      String rtn = HttpUtils.sendGet(url,paraMap);
      System.out.println(rtn);
      ObjectMapper objectMapper = new ObjectMapper();
      objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
      VenueVO returnMessage = objectMapper.readValue(rtn, VenueVO.class);
      dataList =returnMessage.getResults();
      id=returnMessage.getQuery().getMaxId()+1;
      if(dataList.isEmpty()) continue;
      dataList.stream().forEach(s -> s.setSportId(sportId));
      venueNanoService.insertOrUpdateAllColumnBatch(dataList);
    }while(dataList !=null &&dataList.size()>0);

  }

  public void grabMatchNano(HashMap<String,String> paraMap,Integer sportId) throws Exception{

    List<MatchesNano> dataList =null;
    String url ="";
    if(sportId==1){
      url=ConstantFootBallUrl.football_match_schedule_url;
    }else{
      url= ConstantBasketBallUrl.basketball_match_schedule_url;
    }
    String rtn = HttpUtils.sendGet(url,paraMap);
    System.out.println(rtn);
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
    MatchVO returnMessage = objectMapper.readValue(rtn, MatchVO.class);
    MatchRtnVo matchRtnVo =returnMessage.getResults();
    dataList=matchRtnVo.getMatch();
    if(dataList.isEmpty()) return;
    dataList.stream().forEach(s -> s.setSportId(sportId));
    matchesNanoService.insertOrUpdateAllColumnBatch(dataList);

  }


}
